/*
 * OpenGL.java  0.4.0 / Oct 24, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
package com.cute4j.demos.opengl;

import com.cute4j.opengl.GLComponent;
import com.cute4j.swing.Swing;

/**
 * OpenGL Demo.
 *
 * @author Slobodan Vrkacevic
 * @version 0.4.0
 */
public class OpenGL extends GLComponent {

// Private Fields. -------------------------------------------------------------

  private float rtri; // NOPMD
  private float rquad; // NOPMD

// Public Constructors. --------------------------------------------------------

  /**
   * Constructs new OpenGL object.
   */
  public OpenGL() {
    setName("Cute4j OpenGL Demo");
    setSize(640, 480);
  }

// Overridden Methods. ---------------------------------------------------------

  /**
   * {@inheritDoc}
   */
  protected void initializeGL() {
    glShadeModel(GL_SMOOTH);
    glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
    glClearDepth(1.0f);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
  }

  /**
   * {@inheritDoc}
   */
  protected void paintGL() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    glTranslatef(-1.5f, 0.0f, -6.0f);
    glRotatef(rtri, 0.0f, 1.0f, 0.0f);
    glBegin(GL_TRIANGLES);
      glColor3f(1.0f, 0.0f, 0.0f);
      glVertex3f(0.0f, 1.0f, 0.0f);
      glColor3f(0.0f, 1.0f, 0.0f);
      glVertex3f(-1.0f, -1.0f, 0.0f);
      glColor3f(0.0f, 0.0f, 1.0f);
      glVertex3f(1.0f, -1.0f, 0.0f);
    glEnd();
    glTranslatef(3.0f, 0.0f, 0.0f);
    glColor3f(0.5f, 0.5f, 1.0f);
    glRotatef(rquad, 1.0f, 0.0f, 0.0f);
    glBegin(GL_QUADS);
      glVertex3f(-1.0f, 1.0f, 0.0f);
      glVertex3f(1.0f, 1.0f, 0.0f);
      glVertex3f(1.0f, -1.0f, 0.0f);
      glVertex3f(-1.0f, -1.0f, 0.0f);
    glEnd();
    rtri += 0.2f;
    rquad -= 0.15f;
    update();
  }

  /**
   * {@inheritDoc}
   */
  protected void resizeGL(int width, int height) { // NOPMD
    if (height == 0) {
      height = 1;
    }
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0f, width / (float)height, 0.1f, 100.0f);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
  }

// Main Method. ----------------------------------------------------------------

  /**
   * Starts the OpenGL Demo.
   *
   * @param args the application arguments
   */
  public static void main(String[] args) {
    OpenGL openGL = new OpenGL();
    openGL.setVisible(true);
    Swing.startEventLoop();
  }

}
